/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
 *
 *           File:  main.cpp
 *    Description:  Calculate CRC using table. Table generate in compile-time.
 *                  
 *        Created:  18.02.2013 19:57:24
 *         Author:  Aleksey Golovchenko (lexgolovchenko@gmail.com)
 *   Organization:  St.Petersburg State Polytecnical Universisy
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include <stdlib.h>
#include <time.h>
#include <assert.h>
#include <stdint.h>
#include <stdio.h>

#include "crc32.h"
#include "table_gen.h"

/* CRC32 table */
static crc32table<0xEDB88320UL> crc32tbl;

/* Frame */
#define FRAME_WORDS_NUM 16
uint32_t frame[FRAME_WORDS_NUM];        // 0-14 data, 15 - CRC32

int main(int argc, char **argv)
{   
    srand(time(NULL));

    for (int i = 0; i < FRAME_WORDS_NUM - 1; ++i) {
        frame[i] = rand();
    } 

    uint32_t crc = 0xFFFFFFFF;
    int byte_len = 4*(FRAME_WORDS_NUM - 1);
    uint8_t *buf = (uint8_t *)frame;
    while (byte_len--) {
        crc = (crc >> 8) ^ crc32tbl[(crc ^ *buf++) & 0xFF];
    }
    crc ^= 0xFFFFFFFF;    
    frame[FRAME_WORDS_NUM - 1] = crc;    

    // check CRC
    uint32_t crc_check = crc32(frame, 4*(FRAME_WORDS_NUM - 1));
    assert(crc == crc_check);

    printf("CRC32 = 0x%8.8X, CHECK = 0x%8.8X \n", crc, crc_check);

    return 0;
}
